# -*- coding: utf-8 -*-

import logging
from cStringIO import StringIO

__all__= ['logger']

log_stream = StringIO() # Log to stream by default
_lg = logging.StreamHandler(log_stream) # Create a stream handler
level = logging.WARNING # By default log only warnings and higher
_formatter = logging.Formatter('%(asctime)-15s %(levelname)s %(message)s')
_lg.setFormatter(_formatter)

class logger(object):
	'''Class and instance logger descriptor'''
	def __init__(self, class_logger=False):
		self._logger = None
		self._class_logger = class_logger

	def __get__(self, instance, owner):
		global _lg, level
		if self._logger is None:
			if(self._class_logger):
				self._logger = logging.getLogger(owner.__module__ + "." + owner.__name__)
			else:
				self._logger = logging.getLogger(
						instance.__class__.__module__ +
						"." + instance.__class__.__name__ +
						".0x.." + hex(id(instance))[-2:]
						)
			self._logger.setLevel(level)
			self._logger.addHandler(_lg)
		return self._logger
